package com.equipment_management_p1.mq;
import com.equipment_management_p1.config.EquipementRabbitConfig;
import com.pojo.Entity.EquipmentRental;
import com.pojo.mapper.EquipmentMapper;
import com.pojo.mapper.EquipmentRentalMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RabbitListener(queues = EquipementRabbitConfig.DEAD_QUEUE_NAME)
public class ProcessOrder {
        @Autowired
        EquipmentRentalMapper equipmentRentalMapper;
        @Autowired
         RedisTemplate redisTemplate;
        @Autowired
        EquipmentMapper equipmentMapper;
        @RabbitHandler
        public void getReleaseOrderMessage(String message)  {
              Integer erID= Integer.valueOf(message);
              EquipmentRental equipmentRental=equipmentRentalMapper.findByerID(erID);
              if(equipmentRental.getErTYpe()==0){
                  String key = "By_Sum_Key"+String.valueOf(equipmentRental.getRID()) + ":" + String.valueOf(equipmentRental.getEID());
                  Integer sum=(Integer) redisTemplate.opsForValue().get(key);
                  redisTemplate.opsForValue().set(key,sum-equipmentRental.getErnumber());
                  equipmentMapper.modifyQuantity(equipmentRental.getEID(),-equipmentRental.getErnumber());
                  equipmentRental.setErTYpe(2);
                  equipmentRentalMapper.update(equipmentRental);
              }
        }
}
